package com.tao.lab.event.dispath.poster;

import com.tao.lab.event.dispath.Subscription;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: lab
 * @Package: com.tao.lab.event.dispath
 * @ClassName: PendingPost
 * @Description: java类作用描述
 * @Author: wangxf
 * @CreateDate: 2022/5/6 13:32
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/5/6 13:32
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public final class PendingPost {
    private static final List<PendingPost> pendingPostPool = new ArrayList();
    public Object event;
    public Subscription subscription;
    PendingPost next;

    private PendingPost(Object event, Subscription subscription) {
        this.event = event;
        this.subscription = subscription;
    }

    public static PendingPost obtainPendingPost(Subscription subscription, Object event) {
        synchronized (pendingPostPool) {
            int size = pendingPostPool.size();
            if (size > 0) {
                PendingPost pendingPost = (PendingPost) pendingPostPool.remove(size - 1);
                pendingPost.event = event;
                pendingPost.subscription = subscription;
                pendingPost.next = null;
                return pendingPost;
            }
        }

        return new PendingPost(event, subscription);
    }

    public static void releasePendingPost(PendingPost pendingPost) {
        pendingPost.event = null;
        pendingPost.subscription = null;
        pendingPost.next = null;
        synchronized (pendingPostPool) {
            if (pendingPostPool.size() < 10000) {
                pendingPostPool.add(pendingPost);
            }

        }
    }
}
